var $ = require("jquery");

import {changePrice} from './priceaction'

export const CHANGE_BANNER = 'CHANGE_BANNER'
export const CHANGE_CURRENT_INDEX = 'CHANGE_CURRENT_INDEX'
export const CHANGE_HISTORY = 'CHANGE_HISTORY'
export const CHANGE_ADD_STATUS = 'CHANGE_ADD_STATUS'
export const ADD_HISTORY = 'ADD_HISTORY'

export const GET_CONFIG = 'GET_CONFIG'
export const UPLOAD_IMG = 'UPLOAD_IMG'
export const SAVE_DATA = 'SAVE_DATA'
export const EXPORT_CONFIG = 'EXPORT_CONFIG'

export const CHANGE_EXPORT_STATUS = 'CHANGE_EXPORT_STATUS'
export const CHANGE_TEST_STATUS = 'CHANGE_TEST_STATUS'



export function changeBanner(desc, pcUrl, mobileUrl, pcImg, mobileImg){
  return {
    type : CHANGE_BANNER,
    desc,
    pcUrl,
    mobileUrl,
    pcImg,
    mobileImg
  }
}

export function changeCurrentIndex(currentIndex){
  return {
    type : CHANGE_CURRENT_INDEX,
    currentIndex
  }
}

export function changeHistory(history){
  return {
    type : CHANGE_HISTORY,
    history
  }
}

export function changeAddStatus(addStatus){
  return {
    type : CHANGE_ADD_STATUS,
    addStatus
  }
}

export function addHistory(historyItem){
  return {
    type : ADD_HISTORY,
    historyItem
  }
}

export function getConfig(){
  return function (dispatch) {
        $.ajax({
          url : '/api/getconfig',
          dataType : 'json'
        })
        .done(json => {
            var configData = JSON.parse(json);
            // eval(json);
            // var configData = window.configData;
            console.log(configData);
            dispatch(changeHistory(configData.history));
            dispatch(changeCurrentIndex(configData.currentIndex));
            dispatch(changePrice(configData.originalPrice, configData.discountPrice));
            
        })
        .fail(error => {
            console.log(error);
        })
    }
}

export function uploadImg(imgElem, name, cb){
  return function (dispatch) {
        var formData = new FormData();
        formData.append('img', imgElem, name);

        $.ajax({
          url : '/api/uploadimage',
          data : formData,
          processData: false,
          contentType: false,
          method: "POST"
        })
        .done(json => {
            cb && cb();
        })
        .fail(error => {
            console.log(error);
        })
    }
}

export function saveData(dataObj, cb){
  return function (dispatch) {

        $.ajax({
          url : '/api/saveconfig',
          data : {
            dataObj : JSON.stringify(dataObj)
          },
          method: "POST",
          dataType : 'json'
        })
        .done(json => {
            dispatch(changeExportStatus(true));
            cb && cb();
        })
        .fail(error => {
            console.log(error);
        })
    }
}

export function exportConfig(dataObj, cb){
  return function (dispatch) {

        $.ajax({
          url : '/api/exportconfig',
          data : {
            dataObj : JSON.stringify(dataObj)
          },
          method: "POST",
          dataType : 'json'
        })
        .done(json => {
            dispatch(changeExportStatus(false));
            dispatch(changeTestStatus(true));
            cb && cb();
        })
        .fail(error => {
            console.log(error);
        })
    }
}

export function changeExportStatus(exportStatus){
  return {
    type : CHANGE_EXPORT_STATUS,
    exportStatus
  }
}

export function sendToTest(cb){
  return function (dispatch) {

        $.ajax({
          url : '/api/sendtotest',
          method: "POST",
          dataType : 'json'
        })
        .done(json => {
            dispatch(changeTestStatus(false));
            cb && cb();
        })
        .fail(error => {
            console.log(error);
        })
    }
}

export function changeTestStatus(testStatus){
  return {
    type : CHANGE_TEST_STATUS,
    testStatus
  }
}



